<!--
 * @Author       : wfl
 * @LastEditors  : wfl
 * @description  :
 * @updateInfo   :
 * @Date         : 2022-11-23 16:41:31
 * @LastEditTime : 2022-12-08 16:25:11
-->
<script lang="ts" setup name="DelayNode">
import Node from '@main/views/approve-workflow/WorkFlow/tree-node/Node.vue'
import { computed, reactive } from 'vue'

const props = defineProps({
  config: {
    type: Object,
    default: () => {
      return {}
    }
  }
})

const emits = defineEmits<{
  (e: 'selected'): void
  (e: 'insertNode', val: string): void
  (e: 'delNode', node: any): void
}>()

const state = reactive({
  showError: false,
  errorInfo: ''
})

const content = computed(() => {
  if (props.config.props.type === 'FIXED')
    return `等待 ${props.config.props.time} ${getName(props.config.props.unit)}`
  else if (props.config.props.type === 'AUTO')
    return `至当天 ${props.config.props.dateTime}`
  else
    return undefined
})

// 校验数据配置的合法性
const validate = (err: string[]) => {
  state.showError = false
  try {
    if (props.config.props.type === 'AUTO') {
      if ((props.config.props.dateTime || '') === '') {
        state.showError = true
        state.errorInfo = '请选择时间点'
      }
    }
    else {
      if (props.config.props.time <= 0) {
        state.showError = true
        state.errorInfo = '请设置延时时长'
      }
    }
  }
  catch (e) {
    state.showError = true
    state.errorInfo = '配置出现问题'
  }
  if (state.showError)
    err.push(`${props.config.name} 未设置延时规则`)

  return !state.showError
}
const getName = (unit: string) => {
  switch (unit) {
    case 'D':
      return '天'
    case 'H':
      return '小时'
    case 'M':
      return '分钟'
    default:
      return '未知'
  }
}

const handSelected = () => {
  emits('selected')
}
const handInsertNode = (type: string) => {
  emits('insertNode', type)
}
const handDelNode = () => {
  emits('delNode', props?.config)
}

defineExpose({ validate })
</script>

<template>
  <Node
    :title="config.name"
    :show-error="state.showError"
    :content="content"
    :error-info="state.errorInfo"
    placeholder="请设置延时时间"
    header-bgc="#f25643"
    header-icon="ikflow-shijian"
    @selected="handSelected"
    @delNode="handDelNode"
    @insertNode="(type: string) => handInsertNode(type)"
  />
</template>
